package com.shun.base.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shun.base.dto.SysOperLogDto;
import com.shun.base.mapper.SysOperLogMapper;
import com.shun.base.service.ISysOperLogService;
import com.shun.base.vo.SysOperLog;
import com.shun.common.R;
import com.shun.common.page.ShunPage;
import com.shun.common.utils.ShunCommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.stream.Stream;

/**
 * <p>
 * 系统用户操作日志记录 服务实现类
 * </p>
 *
 * @author clever
 * @since 2021-10-10
 */
@Slf4j
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements ISysOperLogService {

    private  final static String ADD_OPER_LOG_ERR = "新增操作日志失败！";

    private  final static String DEL_OPER_LOG_ERR = "删除操作日志失败！";

    @Override
    public R addSysOperLog(SysOperLogDto dto) {
        SysOperLog operLog = ShunCommonUtils.beanMap(dto,SysOperLog.class);
        operLog.setOperId(IdUtil.objectId());
        return save(operLog)?R.suc():R.error(ADD_OPER_LOG_ERR);
    }

    @Override
    public R pageSysOperLog(ShunPage page) {
        log.debug("分页查询参数据：{}",page);
        Page<SysOperLog> sysOperLogPage = page(page.toPage(), (Wrapper<SysOperLog>) Wrappers.query(new SysOperLog()));
        return R.suc(sysOperLogPage);
    }

    @Override
    public R delSysOperLog(String logId) {
        return removeById(logId)?R.suc():R.error(DEL_OPER_LOG_ERR);
    }

    @Override
    public R delSysOperLogs(String[] logIds) {
        Stream.of(logIds).forEach(id ->{removeById(id);});
        return R.suc();
    }
}
